﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebCore.Models;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;

namespace WebCore.Controllers
{
    public class EmpController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public JsonResult GetData(int page, int limit)
        {
            try
            {
                using (DataContext dbContext = new DataContext())
                {
                    var data = from a in dbContext.emp
                               join b in dbContext.role on a.role_id equals b.id
                               select new { a,b.role_name};
                    return Json(new { code = 0, count = data.Count(), data = data.ToList(), msg = "" });
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public IActionResult Add(int? id)
        {
            using (DataContext dbContext = new DataContext())
            {
                emp user = new emp();
                var roles = dbContext.role.ToList();
                if (id >= 0)
                {
                    user = dbContext.emp.FirstOrDefault(t => t.id == id);
                }
                ViewBag.role_id = new SelectList(roles, "id", "role_name", user.role_id);
                return View(user);
            }
        }

        [HttpPost]
        public JsonResult Add(emp model)
        {
            try
            {
                using (DataContext dbContext = new DataContext())
                {
                    model.create_date = DateTime.Now;
                    if (model.id > 0)
                    {
                        var emp = dbContext.emp.FirstOrDefault(t=>t.id==model.id);

                        var user = dbContext.emp.FirstOrDefault(t => t.user_name == model.user_name && t.user_name != emp.user_name);
                        if (user != null)
                            return Json(new { success = false, message = "用户名重复" });

                        var EntityModel = dbContext.Entry(model);
                        dbContext.Set<emp>().Attach(model);
                        EntityModel.State = EntityState.Modified;
                        dbContext.SaveChanges();
                        return Json(new { success = true, message = "修改成功" });
                    }
                    else
                    {
                        model.password = "123456";
                        var user = dbContext.emp.FirstOrDefault(t => t.user_name == model.user_name);
                        if (user != null)
                            return Json(new { success = false, message = "用户名重复" });
                        dbContext.emp.Add(model);
                        dbContext.SaveChanges();
                        return Json(new { success = true, message = "添加成功，初始密码123456" });
                    }
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }


        }
    }
}